package com.zjsru.plan2023.oneday;

/**
 * @Author: CookLee
 * @Date: 2023/8/9
 * 整数的各位积和之差
 * \
 * 输入：n = 234
 * 输出：15
 * 解释：
 * 各位数之积 = 2 * 3 * 4 = 24
 * 各位数之和 = 2 + 3 + 4 = 9
 * 结果 = 24 - 9 = 15
 * \
 * 输入：n = 4421
 * 输出：21
 * 解释：
 * 各位数之积 = 4 * 4 * 2 * 1 = 32
 * 各位数之和 = 4 + 4 + 2 + 1 = 11
 * 结果 = 32 - 11 = 21
 *
 */
public class SubtractProductAndSum {
    
    /**
     * 模拟
     *
     * @param n n
     * @return int
     */
    public int subtractProductAndSum(int n) {
        int sum = 0, product = 1;
        //启用临时变量存储每位数值
        int temp = 0;
        while (n > 0) {
            temp = n % 10;
            n = n / 10;
            sum += temp;
            product *= temp;
        }
        return product - sum;
    }
    
    public static void main(String[] args) {
        SubtractProductAndSum subtractProductAndSum = new SubtractProductAndSum();
        int n = 234;
        System.out.println(subtractProductAndSum.subtractProductAndSum(n));
    }
}
